package com.jinyuan.options.login;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.android.threadpool.IActivity;
import com.android.threadpool.Task;
import com.jinyuan.R;
import com.jinyuan.options.LeftMenuActivity;
import com.jinyuan.options.userinfo.service.UserInfoService;
import com.jinyuan.request.RequestURL;
import com.jinyuan.service.MobileApplication;
import com.jinyuan.service.TaskID;
import com.jinyuan.view.dialog.LoadingDialog;

/**
 * 注销
 * 
 * @author azuryleaves
 * @since 2014-1-3 下午12:14:29
 * @version 1.0
 * 
 */
public class LogoutActivity extends Activity implements IActivity {

	private Button parentdialog_cancel_Button, parentdialog_confirm_Button;
	private LoadingDialog loadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		MobileApplication.allActivity.add(this);
		MobileApplication.allIActivity.add(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logout);
		parentdialog_cancel_Button = (Button) findViewById(R.id.parentdialog_cancel_Button);
		parentdialog_cancel_Button.setOnClickListener(clickListener);
		parentdialog_confirm_Button = (Button) findViewById(R.id.parentdialog_confirm_Button);
		parentdialog_confirm_Button.setOnClickListener(clickListener);
		loadingDialog = new LoadingDialog(this, clickListener);
	}

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.parentdialog_cancel_Button:
				finish();
				break;
			case R.id.parentdialog_confirm_Button:
				Task task = new Task(TaskID.TASK_LOGOUT, RequestURL.getLogout(), this
						.getClass().getName(), "-用户注销-");
				loadingDialog.show();
				MobileApplication.poolManager.addTask(task);
				break;
			case R.id.loadingdialog_cancel_ImageView:
				loadingDialog.cancel();
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void closeLoadingView() {
		
	}

	@Override
	public void init() {
		
	}

	@Override
	public void refresh(int taskId, Object... params) {
		loadingDialog.cancel();
		if (params != null) {
			if (params[0] != null) {
				try {
					JSONObject jsonObject = new JSONObject((String) params[0]);
					boolean logoutState = jsonObject.getBoolean("success");
					if (logoutState) {
						UserInfoService.jsonObject = null;
						LeftMenuActivity.leftmenu_username_TextView.setText(R.string.leftmenu_username_default);
						LeftMenuActivity.logout_Button.setVisibility(View.GONE);
						finish();
					}
					Toast.makeText(this, jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				loadingDialog.showTimeOut();
			}
		} else {
			loadingDialog.showTimeOut();
		}
	}

}
